<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="80px"
            id="form"
          >
            <el-form-item label="姓名：" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="姓名"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="手机：" prop="mobile">
              <el-input
                v-model.trim="form.mobile"
                placeholder="手机号"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getArticleList" :loading="listLoading" :showBtn="false">
            </MainFormBtn>
          </el-form>
        </div>
        <div class="main-controls-wrap">
          <div>
            <el-button type="primary" @click="(addedFormVisible = true), (type = 0)">新增业务员</el-button>
          </div>
        </div>
        <div class="main_table">
          <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list.data"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :height="borderHeight - 36"
          :header-cell-class-name="this.$verticaline"
          :header-cell-style="tableHeightStyle"
          @header-dragend="changeTableWidth"
        >
          <el-table-column label="序号"
          prop="number"
          :width="tableWidth[hash+'number']">
            <template slot-scope="scope">{{
              (current.page - 1) * current.per_page + scope.$index + 1
            }}</template>
          </el-table-column>
          <el-table-column
            label="姓名"
            prop="name"
            :width="tableWidth[hash+'name']"
          />
          <el-table-column
            label="手机"
            prop="mobile"
            :width="tableWidth[hash+'mobile']"
          />
          <el-table-column
            label="状态"
            prop="status"
            :width="tableWidth[hash+'status']"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                @change="
                  (val) => {
                    elswitch(val, scope.row.id, 'status');
                  }
                "
                active-color="#03A98E"
                active-text="开"
                inactive-text="关"
                class="mainor_switch"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="管理操作"
            align="center"
            fixed="right"
            prop="operate"
            :width="tableWidth[hash+'operate'] || 240"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="
                  (addedFormVisible = true),
                    (type = 1),
                    (id = scope.row.id),
                    edit_assig(scope.$index)
                "
                >编辑</el-link
              >
              <el-link
                type="danger"
                @click="(id = scope.row.id), delet(scope.$index)"
                >删除</el-link
              >
            </template>
          </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      :title="type == 1 ? '编辑业务员' : '新增业务员'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog500"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline form-message-mg"
        ref="userListFrom"
        label-width="120px"
      >
        <el-form-item label="姓名：" prop="name">
          <el-input
            v-model.trim="info.name"
            placeholder="姓名"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机：" prop="mobile">
          <el-input
            v-model.trim="info.mobile"
            placeholder="手机"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" :prop="type == 1 ? '' : 'password'">
          <el-input
            v-model.trim="info.password"
            show-password
            :placeholder="type == 1 ? '不填为默认密码' : '填写6位以上的密码'"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  salesman_index,
  salesman_add,
  salesman_edit,
  salesman_delete,
  salesman_changeStatus,
} from "@/api/user/userSalesman";
import rules from "@/rules/user/userSalesman";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  mixins: [mixins],
  components: {},
  data() {
    return {
      // 增加和编辑
      info: {
        name: "",
        mobile: "",
        password: ""
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 搜索
      form: {
        name: "",
        mobile: "",
      },
      // 验证
      rules,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        data: [],
      },
      listLoading: false,
    };
  },
  methods: {
    reset() {
      this.$refs['userListFrom'].resetFields();
      this.salesman_index()
    },
    startGetting() {
      this.getArticleList();
    },
    // 关闭清空表单
    close() {
      this.info = {
        name: "",
        mobile: "",
        password: "",
      };
      this.$refs["userListFrom"].clearValidate();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.salesman_index();
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.salesman_index();
    },
    // 删除
    delet(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除业务员?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          salesman_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              // this.list.data.splice(index, 1);
              this.salesman_index();
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          if (this.type == 1) {
            if (this.info.password != "" && this.info.password.length < 6) {
              this.$message.error("密码为6位以上");
              return;
            }
          }
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(index) {
      this.info = {
        name: this.list.data[index].name,
        password: "",
        mobile: this.list.data[index].mobile,
      };
    },
    // 新增保存
    add() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.info)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.info
      );
      salesman_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.salesman_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 切换状态
    elswitch(val, id, type) {
      let status = val ? 1 : 0;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        {
          id: id,
          status: status,
        }
      );
      salesman_changeStatus(data).then((res) => {
        if (res.errcode == 0) {
          this.$message.success("状态更改成功");
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.info, { id: this.id })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.info,
        { id: this.id }
      );

      salesman_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.salesman_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.salesman_index();
    },
    changePage(page) {
      this.current.page = page;
      this.salesman_index();
    },
    // 列表
    salesman_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        JSON.parse(JSON.stringify(this.form)),
        this.current
      );
      salesman_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            e.status = e.status == 0 ? false : true;
          });
          this.list = res.data.list;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.getNumerical(160);
    this.salesman_index();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 180px);
}
.main_dialog .mainor_form form {
  width: calc(100% - 120px);
}
/deep/ .el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
